﻿using System;
using System.Configuration;

using Norm;

namespace Fuse8.DomainFramework.MongoDbWrapper
{
    public sealed class MongoDbConnection
    {
        #region Singleton

        private static readonly object _syncRoot = new object();

        private static volatile MongoDbConnection _instance;

        public static MongoDbConnection GetInstance()
        {
            if (_instance == null)
            {
                lock (_syncRoot)
                {
                    if (_instance == null)
                    {
                        _instance = new MongoDbConnection();
                    }
                }
            }

            return _instance;
        }

        #endregion

        public string ConnectionString { get; set; }

        private MongoDbConnection()
        {
            var connectionString = ConfigurationManager.AppSettings["MongoDbConnectionString"];

            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ConfigurationErrorsException("MongoDbConnectionString isn't defined in config file");
            }
            else
            {
                ConnectionString = connectionString;
            }
        }
    }
}
